var appInstance = getApp();
Page({
	data:{
		apiData: null,//接口地址
		price: null,//计算的价格
		pInfo: null,//产品信息
		selectedInfo: null//选择信息
	},
	onLoad: function(argv){

	  var that = this;
      that.data.apiData = appInstance.globalData.apiData;

		var params = argv;
		var pid = params.pid;
		
		var urls = '';
		var paramObj = {};
		for (var key in params) {

			urls += key + '=' + params[key]+'&';
			if(key!='pid'){

				paramObj[key] = params[key];
			}
		}

		var resInfo = calcPrice(pid, paramObj);
		that.setData({
        	price: resInfo.price,
        	pInfo: resInfo.pInfo,
        	selectedInfo: resInfo.selectedInfo
        })

		var showTitle = resInfo.pInfo.title+'—报价计算结果';
         wx.setNavigationBarTitle({
        	title: showTitle
        })
		//urls = urls.substring(0, urls.lastIndexOf('&'));
		// var apiUrl = that.data.apiData.calcPrice+'&'+urls;
		// wx.request({
		// 	url: apiUrl,
		// 	success: function(res){
		// 		var resInfo = res.data;
	 //            if(resInfo.code!='0000'){

	 //                wx.showModal({
	 //                  title: '提示',
	 //                  content: '数据获取失败',
	 //                  showCancel: false
	 //                })
	 //                return false;
	 //            }
	 //            that.setData({
	 //            	price: resInfo.price,
	 //            	pInfo: resInfo.pInfo,
	 //            	selectedInfo: resInfo.selectedInfo
	 //            })
		// 	}
		// });
	},
	callPhone: function(){
		var phoneNumber = appInstance.globalData.servicePhone;
		wx.makePhoneCall({
			phoneNumber:phoneNumber
		});
	}
})

function calcPrice(pid, obj){

	var pInfo = appInstance.globalData.typeConf[pid];
	var itemDetailConf = appInstance.globalData.itemsDetailConf;

	var price = parseFloat(1000*Math.random());
	var priceNum = '￥'+price.toFixed(2)

	var selectedInfo = {};
	if(obj){
		var itemsConf = appInstance.globalData.itemsConf[pid];
		for(var k in obj){

			var v = obj[k];
			var kStr = pid + '.' + k;
			var detailInfo = getObjValues(itemDetailConf[kStr]);
			
			selectedInfo[k] = {
				value: detailInfo[v],
				item : itemsConf[k]
			}
		}	
	}
	return {
		code: '0000',
		price: priceNum,
		pInfo: pInfo,
		selectedInfo: selectedInfo
	}
}
function getObjValues(obj){

	var returnValues = [];
	var i = 0;
	for(var key in obj){
		returnValues[i++] = obj[key];
	}
	return returnValues;
}
